// pages/chat_list.js
import {im_getConversationList} from '../../utils/tim_utils'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userID:'',//从app.globalData获取

    isAdvising: '',//从服务器获取
    nowAdvising:{},//从服务器获取

    nowConversation:'',//腾讯IM获取总会话列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      userID: app.globalData.userInfo.id,
      isAdvising: app.globalData.isAdvising,
      nowAdvising: app.globalData.nowAdvising
    })

    wx.event.on('conversationListRender',(conversations)=>{
      for(var i=0; i<conversations.length; i++){
        if(conversations[i]['conversationID']=="C2C"+this.data.nowAdvising.advisorInfo.id){
          this.setData({
            nowConversation: conversations[i]
          })
        }
      }
    })
    wx.event.on('SDKReady',()=>{//SDK启动成功后获取会话列表
      im_getConversationList();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // get_nowAdvising()//接入后端接口后应取消注释
    this.setData({
      isAdvising: app.globalData.isAdvising,
      nowAdvising: app.globalData.nowAdvising
    })
    if(this.data.isAdvising){
      im_getConversationList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  chatTest: function(){
    var that = this
    wx.navigateTo({
      url: '../chat/chat?nowAdvisingBean='+JSON.stringify(this.data.nowAdvising),
    })
  },

  stamp2time(timestamp){
    var newDate = new Date();
    newDate.setTime(timestamp);
    return newDate.toLocaleString()
  }
})